package admin.controllers;

import java.io.IOException;
import java.net.URLDecoder;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import models.LinkModel;

import admin.data.LinkData;
import debug.LinkDebug;

public class DeleteLinkServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
	throws IOException {
	response.setContentType("text/html");
	String path = ((HttpServletRequest) request).getRequestURI();
	long linkId = 0;
	try {
		linkId = Long.parseLong(URLDecoder.decode(path.toLowerCase().replaceAll("/admin/delete-link/", ""),"UTF-8"));
	} catch (Exception e) {
		response.sendRedirect("/admin/list-link");
	}
	try {
		LinkData linkData = new LinkData();
		LinkModel link = linkData.getLink(linkId);
		linkData.delete(link);
		LinkDebug.debugWeb(link, response);
	} catch (Exception e) {
		// TODO: handle exception
	}
	response.sendRedirect("/admin/list-link");
}
}
